import moment from 'moment';
import React from 'react';

export const useEchartsData = (info, loadInfo) => {
  const infoData = info?.[loadInfo];
  if (JSON.stringify(info?.[loadInfo]) == '{}' || !info?.[loadInfo]) return [];
  let infoDataList: Record<string, any>[] = [];
  let infoKeys: string[] = Object?.keys(infoData);
  infoKeys?.sort((a: any, b: any) => {
    return moment(a)?.isBefore(moment(b)) ? -1 : 1;
  });
  infoKeys?.forEach((ite) => {
    let totalSum = 0;
    let totalPaid = 0;
    let totalResidue = 0;
    infoData[ite]?.forEach((item) => {
      totalSum += Number(item?.totalSum) || 0;
      totalPaid += Number(item?.totalPaid) || 0;
      totalResidue += Number(item?.totalResidue) || 0;
    });
    const obj = {
      name: ite,
      totalSum,
      totalPaid,
      totalResidue,
    };
    infoDataList?.push(obj);
  });
  return infoDataList;
};
export const useEchartsDataAmount = (info, loadInfo) => {
  const infoData = info?.[loadInfo];
  if (JSON.stringify(info?.[loadInfo]) == '{}' || !info?.[loadInfo]) return [];
  let infoDataList: Record<string, any>[] = [];
  let infoKeys: string[] = Object?.keys(infoData);
  infoKeys?.sort((a: any, b: any) => {
    return moment(a)?.isBefore(moment(b)) ? -1 : 1;
  });
  infoKeys?.forEach((ite) => {
    let expectAllSum = 0;
    let expectCompleteSum = 0;
    // let totalResidue = 0;
    infoData[ite]?.forEach((item) => {
      expectAllSum += Number(item?.expectAllSum) || 0;
      expectCompleteSum += Number(item?.expectCompleteSum) || 0;
      // totalResidue += Number(item?.totalResidue) || 0;
    });
    const obj = {
      name: ite,
      expectAllSum,
      expectCompleteSum,
      // totalResidue,
    };
    infoDataList?.push(obj);
  });
  return infoDataList;
};
export const useEchartsDataReport = (info, loadInfo) => {
  const infoData = info?.[loadInfo];
  if (JSON.stringify(info?.[loadInfo]) == '{}' || !info?.[loadInfo]) return [];
  let infoDataList: Record<string, any>[] = [];
  let infoKeys: string[] = Object?.keys(infoData);
  infoKeys?.sort((a: any, b: any) => {
    return moment(a)?.isBefore(moment(b)) ? -1 : 1;
  });
  infoKeys?.forEach((ite) => {
    // 总预算金额
    let expectAllSum = 0;
    let totalNum = 0;
    let searchState: any = [];
    let searchStateName: any = [];
    // 每一条数据
    let stateInfo: any = [];
    infoData[ite]?.forEach((item) => {
      expectAllSum += Number(item?.expectAllSum) || 0;
      totalNum += Number(item?.totalNum) || 0;
      searchState.push(item?.searchState || null);
      searchStateName.push(item?.searchStateName || '');
      stateInfo.push({
        expectAllSum: Number(item?.expectAllSum) || 0,
        totalNum: Number(item?.totalNum) || 0,
        searchState: item?.searchState || null,
        searchStateName: item?.searchStateName || '',
      });
    });
    const obj = {
      name: ite,
      totalNum,
      expectAllSum,
      searchState,
      searchStateName,
      stateInfo,
      // totalResidue,
    };
    infoDataList?.push(obj);
  });
  return infoDataList;
};
